package com.daoeping.dao;

import com.daoeping.data.Bzn_question;
import com.daoeping.data.Bzn_questionfollow;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;
import com.daoeping.util.TimeDiff;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;




public class Bzn_questionfollowDAO extends AllDaoSupport {
	public Bzn_questionfollow findBzn_questionfollowById(Bzn_questionfollow bzn_questionfollow){
		List<Bzn_questionfollow> bzn_questionList = (List<Bzn_questionfollow>) this.selectDataByHQL("from Bzn_questionfollow bzn where bzn.bzn_question.id = "+bzn_questionfollow.getBzn_question().getId()+" and bzn.usr_user.id="+bzn_questionfollow.getUsr_user().getId());
		if(bzn_questionList.size()==1){
			return bzn_questionList.get(0);
		}else{
			return null;
		}
	}
	
	public boolean findBzn_questionfollowByOtherId(Bzn_questionfollow bzn_questionfollow){
		List<Bzn_questionfollow> bzn_questionList = (List<Bzn_questionfollow>) this.selectDataByHQL("from Bzn_questionfollow bzn where bzn.bzn_question.id = "+bzn_questionfollow.getBzn_question().getId()+" and bzn.usr_user.id !="+bzn_questionfollow.getUsr_user().getId());
		if(bzn_questionList.size()>=1){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean delBzn_questionfollowByQuestionId(Bzn_question bzn_question){
		return updateDataByHQL("delete from Bzn_questionfollow follow where follow.bzn_question.id ="+bzn_question.getId());
	} 
	
	
	public JsonData findAllBzn_questionfollowByPage (Page page, TableData tableData , Bzn_question bzn_question) {
		try {
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();//on follow.bzn_question.id = question.id on user.id = follow.usr_user.id
			sb1.append("select  question.title , question.content , user.username,  question.id , user.id ,question.createDatetime ,  question.usr_user.id , follow.id , question.updateDatetime  from Bzn_questionfollow follow left join  follow.bzn_question  question "
			+"  left join follow.usr_user user  where question.flag = '"+bzn_question.getFlag()+"' and (question.title like '%"+bzn_question.getTitle()+"%' or question.content like '%"+bzn_question.getTitle()+"%') and follow.usr_user.id = "+bzn_question.getUsr_user().getId());
			sb2.append("select  count(follow) from Bzn_questionfollow follow left join  follow.bzn_question  question "
					+"  left join follow.usr_user user where  question.flag = '"+bzn_question.getFlag()+"' and (question.title like '%"+bzn_question.getTitle()+"%' or question.content like '%"+bzn_question.getTitle()+"%') and follow.usr_user.id = "+bzn_question.getUsr_user().getId());//
			if(bzn_question.getBsc_category()!=null&&!bzn_question.getBsc_category().getId().equals("")){
				sb1.append(" and question.bsc_category.id = "+bzn_question.getBsc_category().getId());
				sb2.append(" and question.bsc_category.id = "+bzn_question.getBsc_category().getId());
			}
			sb1.append(" order by question.id desc");
			List<Object[]> resultlist = (List<Object[]>) this.getPageList(
					page, sb1.toString());	
			List<Object[]> newlist = new ArrayList<Object[]>();
			for (Object[] objects : resultlist) {
				Object[] newObjects = new Object[8];
				for (int i = 0; i < newObjects.length; i++) {
					newObjects[i] = objects[i];
				}
				newObjects[5] = new TimeDiff().getTimeDiff(objects[5]+"", objects[8]+"");
				System.out.println(objects[4]+"=============="+objects[6]);
				if(objects[4]!=objects[6]){
					newObjects[6] = "cancelconcern";
				}
				newlist.add(newObjects);
			}
			JSONArray json = new ConverntJsonData().getJsonData(newlist);
			JSONArray newJson = new JSONArray();
			newJson.put(this.getNum(page, sb2.toString()));
			for (int i = 0; i < json.length(); i++) {
				newJson.put(json.get(i));
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			System.out.println(newJson);
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
}


